package com.yanqu.road.server.manger.activity.recycle;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityItemRecycleData;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.activity.eActivityCookBoyType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.RelationActivityMgr;
import com.yanqu.road.server.manger.activity.busyfarming.BusyFarmingMgr;
import com.yanqu.road.server.manger.activity.cookboy.CookBoyActivityMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

public class CookBoyRecycleStragety extends AbstractItemRecycleStrategy {
    @Override
    public void markNeedRecycleItem() {
        ActivityInfo cookBoy1Info = RelationActivityMgr.getActivityInfo(eActivityType.CookingBoyActivity.getValue(), eActivityCookBoyType.Process1.getChildType());
        if (cookBoy1Info != null) {
            int itemId = CookBoyActivityMgr.getCookBoyHpItemId(cookBoy1Info.getActivityId());
            long recycleTime = cookBoy1Info.getEndTime() * 1000L + 600 * 1000;
            List<String> paramList = new ArrayList<>();
            paramList.add(String.valueOf(CookBoyActivityMgr.getCookBoyIntegralItemId(cookBoy1Info.getActivityId())));
            ActivityItemRecycleMgr.addRecycleItemData(itemId, activityType, 1, recycleTime, paramList);
        }
        ActivityInfo cookBoy2Ac = RelationActivityMgr.getActivityInfo(eActivityType.CookingBoyActivity.getValue(), eActivityCookBoyType.Process2.getChildType());
        if (cookBoy2Ac != null) {
            int itemId = CookBoyActivityMgr.getCookBoy2PickUpItemId(cookBoy2Ac.getActivityId());
            long recycleTime = cookBoy2Ac.getEndTime() * 1000L + 600 * 1000;
            List<String> paramList = new ArrayList<>();
            paramList.add(String.valueOf(CookBoyActivityMgr.getCookBoy2IntegralItemId(cookBoy2Ac.getActivityId())));
            ActivityItemRecycleMgr.addRecycleItemData(itemId, activityType, 2, recycleTime, paramList);
        }
    }

    @Override
    public boolean isActivityRunning() {
        ActivityInfo activityInfo = BusyFarmingMgr.getActivityInfo();
        return ActivityMgr.activityInTime(activityInfo);
    }

    @Override
    protected void initActivityType() {
        activityType = eActivityType.CookingBoyActivity.getValue();
    }

    @Override
    protected void doRecycle(long userId, ActivityItemRecycleData data) {
        int recycleType = data.getRecycleType();
        if (recycleType == 1) {
            recycleCookBoy1Item(userId, data);
        } else if (recycleType == 2) {
            recycleCookBoy2Item(userId, data);
        }
    }

    private void recycleCookBoy2Item(long userId, ActivityItemRecycleData data) {
        int hpItemId = data.getItemId();
        List<String> paramList = data.getParamList();
        int integralItemId = Integer.parseInt(paramList.get(0));
        UserBagItem userBagPickUpItem = getUserBagItem(userId, hpItemId);
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(hpItemId);
        if (userBagPickUpItem != null && userBagPickUpItem.getNum() > 0) {
            long num = userBagPickUpItem.getNum();
            if (removeItem(userBagPickUpItem)) {
                String language = UserMgr.getUserInfo(userId).getLanguage();
                String integralNum = BigInteger.valueOf(num).multiply(goodsInfo.getParamList().get(1)).toString();
                String reward = integralItemId + "=" + integralNum;
                String mailTitle = MultipleLanguageMgr.getContent(MailManager.COOK_BOY2_ITEM_RECOVERY_TITLE, language);
                String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.COOK_BOY2_ITEM_RECOVERY_CONTENT, language),
                        num, integralNum);
                MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), reward, mailContent, mailTitle);
            }
        }
    }

    private void recycleCookBoy1Item(long userId, ActivityItemRecycleData data) {
        int hpItemId = data.getItemId();
        List<String> paramList = data.getParamList();
        int integralItemId = Integer.parseInt(paramList.get(0));
        UserBagItem userBagHpItem = getUserBagItem(userId, hpItemId);
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(hpItemId);
        if (userBagHpItem != null && userBagHpItem.getNum() > 0) {
            long num = userBagHpItem.getNum();
            if (removeItem(userBagHpItem)) {
                String language = UserMgr.getUserInfo(userId).getLanguage();
                String integralNum = BigInteger.valueOf(num).multiply(goodsInfo.getParamList().get(1)).toString();
                String reward = integralItemId + "=" + integralNum;
                String mailTitle = MultipleLanguageMgr.getContent(MailManager.COOKBOY_RETURN_ITEM_TITLE, language);
                String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.COOKBOY_RETURN_ITEM_CONTENT, language),
                        num, integralNum);
                MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), reward, mailContent, mailTitle);
            }
        }
    }
}
